package com.ts.api.http.market.channel;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarket;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarketConvertType;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.market.entity.ChannelMarketParameter;
import com.ts.api.module.api.convert.market.handler.ChannelMarketConvertHandler;
import com.ts.api.module.api.entity.ApiUserRequest;

import java.util.Map;

/**
 *  放鑫钱包(晚上)
 *
 *  线上联登用这个文档  https://kcnkoithehhs.feishu.cn/wiki/Cgu6wLYPsiCzWykqA05cJdqsnig?from=from_copylink
 *
 *  @author WJB
 */
@ApiChannelMarket(uniqueCode = "1486e3620ec044ebb2c0abad5bf24319", type = ApiChannelMarketConvertType.DATA_EXCHANGE_PHONE_MASK_8)
public class FangXinQianBaoYePhoneMask implements ChannelMarketConvertHandler {

    private static final String KEY = "9GI8m1HAU%3ZTRCU";

    @Override
    public ApiUserRequest parse(RequestVO requestVO) {
        AES aes = AES.createCbc(KEY.getBytes(), KEY.getBytes());
        String requestData = aes.decryptFromBase64Str(requestVO.getStr("requestData"));
        JSONObject object = JSONObject.parseObject(requestData);

        ApiUserRequest apiUserRequest = new ApiUserRequest();
        String phone = object.getString("encryptValue");
        apiUserRequest.setPhone(phone);
        return apiUserRequest;
    }

    @Override
    public Map<String, Object> response(ChannelMarketParameter channelMarketParameter, ApiBizStatus apiBizStatus) {
        if (apiBizStatus.hasBizSuc()) {
            return MapUtils.newHashMap(newMap -> {
                newMap.put("code", 1);
                newMap.put("msg", apiBizStatus.getBizMsg());
                newMap.put("data", channelMarketParameter.getListPhoneMd5());
            });
        }

        return MapUtils.newHashMap(newMap -> {
            newMap.put("code", -1);
            newMap.put("msg", apiBizStatus.getBizMsg());
            newMap.put("data", null);
        });
    }

}
